/************************************************************************
This file is part of lpc2xxx package for the simpleRTJ virtual machine.

This file is covered by the GNU GPL with the following exception:
  As a special exception, the copyright holders of this library give you permission
  to link this library with independent modules to produce an executable, regardless
  of the license terms of these independent modules, and to copy and distribute the
  resulting executable under terms of your choice, provided that you also meet, for
  each linked independent module, the terms and conditions of the license of that
  module. An independent module is a module which is not derived from or based on
  this library. If you modify this library, you may extend this exception to your
  version of the library, but you are not obligated to do so. If you do not wish
  to do so, delete this exception statement from your version.


THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL RTJ COMPUTING BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Copyright (c) 2000-2006 RTJ Computing Pty. Ltd. All rights reserved.
***********************************************************************/
package lpc2xxx.hw;


/**
 * Class providing access to six PWM (Pulse Width Modulation) outputs on the LPC21xx.<br>
 * The following PWN channels are available on LPC21xx devices:<br>
 * <code>
 * PWM1 - GPIO pin P0.0<br>
 * PWM2 - GPIO pin P0.7<br>
 * PWM3 - GPIO pin P0.1<br>
 * PWM4 - GPIO pin P0.8<br>
 * PWM5 - GPIO pin P0.21<br>
 * PWM6 - GPIO pin P0.9<br>
 * </code><br>
 * PWM class provides only basic access to the PWM module. Frequency is common to all PWM outputs,
 * however, duty cycle can be separately set for each output. Only single edge PWM mode is used.
 * <p>
 * Add your own code to implement additional features if required.
 */
public class PWM
{
    /** PWM output 1 (P0.0) */
    public final static int PWM1 = 2;
    /** PWM output 2 (P0.7) */
    public final static int PWM2 = 4;
    /** PWM output 3 (P0.1) */
    public final static int PWM3 = 8;
    /** PWM output 4 (P0.8) */
    public final static int PWM4 = 16;
    /** PWM output 5 (P0.21) */
    public final static int PWM5 = 32;
    /** PWM output 6 (P0.9) */
    public final static int PWM6 = 64;

    /** PWM output pins */
    private static int pwms;

    /**
     * Hidden constructor - can't be instantiated. Accessed only via provided
     * static methods.
     */
    private PWM()
    {
    }

    /**
     * PWM constructor, creates an instance of this object.<br>
     * Selected PWM output will be initialised to specified frequency and duty cycle.
     * @param pwm PWM channel(s) to initialize. Use one of the PWMx constants, multiple channels
     *            can be combined using <i>or</i> operator.    
     * @param freq initial frequency (in Hz) for the PWM. 
     */
    public static void init(int pwm, int freq)
    {
        pwms |= pwm;
        init0(pwm, freq);
        if ((pwm & PWM1) == PWM1)
            GPIO.config(0, 0xFFFFFFFC, 0x00000002);
        if ((pwm & PWM2) == PWM2)
            GPIO.config(0, 0xFFFF3FFF, 0x00008000);
        if ((pwm & PWM3) == PWM3)
            GPIO.config(0, 0xFFFFFFF3, 0x00000008);
        if ((pwm & PWM4) == PWM4)
            GPIO.config(0, 0xFFFCFFFF, 0x00020000);
        if ((pwm & PWM5) == PWM5)
            GPIO.config(1, 0xFFFFF3FF, 0x00000400);
        if ((pwm & PWM6) == PWM6)
            GPIO.config(0, 0xFFF3FFFF, 0x00080000);
    }

    /**
     * Applies (activates) previously set duty cycles.
     */
    public static void apply()
    {
        apply(pwms);
    }

    /**
     * Sets the new duty cycle for the PWM output. Use <code>apply</code> method
     * to actually activate new duty cycle.
     * @param dc new duty cycle of the PWM channel. Acceptable range is 0-10000.
     *        Each step represents 0.01% change in duty cycle.
     */
    public static native void duty(int pwms, int dc);

    /**
     * Sets the new frequency for PWM. Note that frequency cange affects all PWM outputs.
     * @param fq new frequency (in Hz) for PWM
     */
    public static native void freq(int fq);

    /** Initializes the PWM module and sets the specified frequency */
    private static native void init0(int pwms, int freq);
    /** Applies new duty cycle for the specified PWM channels. */
    private static native void apply(int pwms);
}
